package dao;

import hibernateUtils.HibernateUtil;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import beans.ProovedorBean;

public class HibernateProveedorDAO {
	private static HibernateProveedorDAO instancia = null;
	private static SessionFactory sf = null;

	public static HibernateProveedorDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateProveedorDAO();
		}
		return instancia;
	}

	public void grabarProveedor(ProovedorBean v) {

		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(v);
		;
		session.flush();
		session.getTransaction().commit();
		session.close();

	}

	public ProovedorBean getProveedor(ProovedorBean p) {
		Session session = sf.openSession();
		Query q = session.createQuery(
				"from ProovedorBean v where v.idProveedor=? ").setInteger(0,
				p.getIdProveedor());
		q.setFirstResult(0).setMaxResults(1);
		Object o = q.uniqueResult();
		ProovedorBean proveedor = (ProovedorBean) o;
		return proveedor;

	}

	public void updateProveedor(ProovedorBean vb) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.update(vb);
		;
		session.flush();
		session.getTransaction().commit();
		session.close();

	}

	public List<ProovedorBean> list() {
		Session sesion = sf.openSession();
		sesion.beginTransaction();
		List<ProovedorBean> proveedores = sesion.createQuery(
				"from ProovedorBean").list();
		return proveedores;

	}
}
